package com.etoak.config;

import com.etoak.properties.ImageProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class ETConfig implements WebMvcConfigurer {

    @Autowired
    ImageProperties imageProperties;

    /**
     * <mvc:resources mapping="/pics/**" location="file:d:/upload/et2409/" />
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        String mapping = imageProperties.getPrefix().endsWith("/") ?
                imageProperties.getPrefix() + "**" :
                imageProperties.getPrefix() + "/**";

        String location = imageProperties.getLocation().endsWith("/") ?
                "file:" + imageProperties.getLocation() :
                "file:" + imageProperties.getLocation() + "/";

        registry.addResourceHandler(mapping).addResourceLocations(location);
    }
}
